package com.mt.channel.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.channel.entity.ChannelApplicationEntity;
import com.mt.channel.mapper.ChannelApplicationEntityMapper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;

@Component
public class ChannelApplicationServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(ChannelApplicationServiceModel.class);

    @Autowired
    private ChannelApplicationEntityMapper channelApplicationEntityMapper;

    public ServiceResult<List<ChannelApplicationEntity>> selectList(Map<String, Object> params,
        PagerInfo<?> pagerInfo) {
        Assert.notNull(this.channelApplicationEntityMapper, "Property 'channelApplicationEntityMapper' is required.");
        ServiceResult<List<ChannelApplicationEntity>> result = new ServiceResult<List<ChannelApplicationEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }
            result.setResult(this.channelApplicationEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelApplicationServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<ChannelApplicationEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.channelApplicationEntityMapper, "Property 'channelApplicationEntityMapper' is required.");
        ServiceResult<ChannelApplicationEntity> result = new ServiceResult<ChannelApplicationEntity>();
        try {
            result.setResult(this.channelApplicationEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelApplicationServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(ChannelApplicationEntity channelApplicationEntity) {
        Assert.notNull(this.channelApplicationEntityMapper, "Property 'channelApplicationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelApplicationEntityMapper.insert(channelApplicationEntity);
            if (save > 0) {
                id = channelApplicationEntity.getChannelApplicationId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelApplicationServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> updateRelate(ChannelApplicationEntity channelApplicationEntity) {
        Assert.notNull(this.channelApplicationEntityMapper, "Property 'channelApplicationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelApplicationEntityMapper.updateByPrimaryKeySelective(channelApplicationEntity);
            if (save > 0) {
                id = channelApplicationEntity.getChannelApplicationId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelApplicationServiceModel][updateByPrimaryKeySelective]:query update occur exception",
                e);
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(ChannelApplicationEntity channelApplicationEntity) {
        Assert.notNull(this.channelApplicationEntityMapper, "Property 'channelApplicationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.channelApplicationEntityMapper.updateByPrimaryKeySelective(channelApplicationEntity);
            if (save > 0) {
                id = channelApplicationEntity.getChannelApplicationId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelApplicationServiceModel][updateByPrimaryKeySelective]:query update occur exception",
                e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer channelApplicationId) {
        Assert.notNull(this.channelApplicationEntityMapper, "Property 'channelApplicationEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.channelApplicationEntityMapper.deleteByPrimaryKey(channelApplicationId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[ChannelApplicationServiceModel][updateByPrimaryKeySelective]:query update occur exception",
                e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }
}
